package com.itcm.tcpdemo.components;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JsonResponseBuilder {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static String buildCommandResponse(List<Map<String, Object>> actions) throws Exception {
        Map<String, Object> response = new HashMap<>();
        response.put("event", "message");
        response.put("type", "command");

        Map<String, Object> details = new HashMap<>();
        details.put("actions", actions);
        response.put("details", details);

        return objectMapper.writeValueAsString(response);
    }

    public static List<String> buildChatResponses(String[] answers, String ttsProvider, double speedRatio, String voiceType, double pitchRatio) throws Exception {
        List<String> responses = new ArrayList<>();

        for (String answer : answers) {
            Map<String, Object> response = new HashMap<>();
            response.put("event", "message");
            response.put("type", "chat");

            Map<String, Object> details = new HashMap<>();
            details.put("answer", answer);
            details.put("tts_provider", ttsProvider);
            details.put("speed_ratio", speedRatio);
            details.put("voice_type", voiceType);
            details.put("pitch_ratio", pitchRatio);
            response.put("details", details);

            responses.add(objectMapper.writeValueAsString(response));
        }

        return responses;
    }

    public static String buildErrorResponse(String reason, String msg) throws Exception {
        Map<String, Object> response = new HashMap<>();
        response.put("event", "message");
        response.put("type", "error");

        Map<String, Object> details = new HashMap<>();
        details.put("reason", reason);
        details.put("msg", msg);
        response.put("details", details);

        return objectMapper.writeValueAsString(response);
    }

    public static String buildEndResponse() throws Exception {
        Map<String, Object> response = new HashMap<>();
        response.put("event", "message_end");

        return objectMapper.writeValueAsString(response);
    }
}
